package com.yeziji.constant;

import com.yeziji.common.CommonErrorMsg;
import com.yeziji.common.msg.DataMessageErrorMsg;
import com.yeziji.common.rabbitMq.RabbitMqTopicEnum;
import com.yeziji.exception.ApiException;
import com.yeziji.utils.expansion.Asserts;
import com.yeziji.utils.expansion.Lists2;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Map;
import java.util.Objects;

/**
 * 数据消息主题处理器
 *
 * @author hwy
 * @since 2024/10/27 1:41
 **/
@Getter
@AllArgsConstructor
public enum DataMessageTopicEnum implements RabbitMqTopicEnum {
    /**
     * 发生数据消息主题
     */
    // 一般而言该死信枚举只作为标识, 各个队列的死信系统会自动创建.
    DEAD_LETTER(0, "dead.letter.exchange", RabbitMqExchangeTypeEnum.DIRECT, "dead.letter.route", "dead.letter.queue", null, "死信标识"),
    PLATFORM_DATA_INSERT_SYNC(1, "platform.db.operate.exchange", RabbitMqExchangeTypeEnum.TOPIC, "platform.db.operate.insert", "platform.db.operate.insert-queue", null, "数据中心平台新增数据"),
    PLATFORM_DATA_UPDATE_SYNC(2, "platform.db.operate.exchange", RabbitMqExchangeTypeEnum.TOPIC, "platform.db.operate.update", "platform.db.operate.update-queue", null, "数据中心平台更新数据"),
    PLATFORM_DATA_DELETE_SYNC(3, "platform.db.operate.exchange", RabbitMqExchangeTypeEnum.TOPIC, "platform.db.operate.delete", "platform.db.operate.delete-queue", null, "数据中心平台删除数据"),
    PLATFORM_DATA_CONFIRM_SYNC(4, "platform.db.operate.exchange", RabbitMqExchangeTypeEnum.TOPIC, "platform.db.operate.confirm", "platform.db.operate.confirm-queue", null, "数据中心平台变更确认"),
    ;

    private final int code;
    private final String exchange;
    private final RabbitMqExchangeTypeEnum exchangeType;
    private final String routeKey;
    private final String queue;
    private final Map<String, Object> args;
    private final String desc;

    /**
     * 根据枚举名称获取枚举
     *
     * @param name 枚举名称
     * @return {@link DataMessageTopicEnum}
     */
    public static DataMessageTopicEnum getByName(String name) {
        Asserts.notBlank(name, CommonErrorMsg.REQUIRED_DATA_IS_NULL);

        return Lists2.filterFirstOpt(DataMessageTopicEnum.values(), statusEnum -> Objects.equals(statusEnum.toString(), name))
                .orElse(null);
    }

    /**
     * 通过交换机和路由名称获取其行为枚举
     *
     * @param exchange 交换机名称
     * @param routeKey 路由名称
     * @return {@link DataMessageTopicEnum}
     */
    public static DataMessageTopicEnum getByExchangeAndRouteKey(String exchange, String routeKey) {
        return Lists2.filterFirstOpt(
                        DataMessageTopicEnum.values(),
                        statusEnum -> Objects.equals(statusEnum.getExchange(), exchange) && Objects.equals(statusEnum.getRouteKey(), routeKey)
                )
                .orElse(null);
    }

    /**
     * 获取操作行为的交换器
     *
     * @param exchange 交换机名称
     * @return {@link DataMessageTopicEnum}
     * @see DataOperateTypeEnum
     */
    public static DataMessageTopicEnum getByOperateExchange(String exchange) {
        return Lists2.filterFirstOpt(
                        DataMessageTopicEnum.values(),
                        (statusEnum) -> {
                            try {
                                DataOperateTypeEnum operateTypeEnum = DataOperateTypeEnum.getByCode(statusEnum.getCode() - 1);
                                return Objects.equals(statusEnum.getExchange(), exchange) && operateTypeEnum != null;
                            } catch (Exception e) {
                                return false;
                            }
                        }
                )
                .orElse(null);
    }
}